﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Browis.Utils
{
    public class HashStream : Stream
    {
        private Stream innerStream;
        private bool leaveInnerStreamOpen;
        private HashAlgorithm hashAlgorithm;
        private FileAccess mode;
        private bool final;

        /// <summary>
        /// 构造哈希流
        /// </summary>
        /// <param name="innerStream">获取需要构造哈希流的实例</param>
        /// <param name="leaveInnerStreamOpen">指示释放时是否保持流不关闭</param>
        /// <param name="hashAlgorithm">使用的哈希算法</param>
        public HashStream(Stream innerStream, bool leaveInnerStreamOpen, HashAlgorithm hashAlgorithm)
        {
            if (innerStream == null)
                throw new ArgumentNullException("innerStream");

            if (hashAlgorithm == null)
                throw new ArgumentNullException("hashAlgorithm");

            this.innerStream = innerStream;
            this.leaveInnerStreamOpen = leaveInnerStreamOpen;
            this.hashAlgorithm = hashAlgorithm;
            this.mode = FileAccess.ReadWrite;
            this.final = false;
        }

        public override bool CanRead
        {
            get { return innerStream.CanRead; }
        }

        public override bool CanSeek
        {
            get { return innerStream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return innerStream.CanWrite; }
        }

        public override void Flush()
        {
            innerStream.Flush();
        }

        public override long Length
        {
            get { return innerStream.Length; }
        }

        public override long Position
        {
            get
            {
                return innerStream.Position;
            }
            set
            {
                innerStream.Position = value;
            }
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return innerStream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            innerStream.SetLength(value);
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            if (mode != FileAccess.Read)
            {
                hashAlgorithm.Initialize();
                mode = FileAccess.Read;
                final = false;
            }

            int bytesRead = innerStream.Read(buffer, offset, count);
            hashAlgorithm.TransformBlock(buffer, offset, bytesRead, null, 0);
            return bytesRead;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            if (mode != FileAccess.Write)
            {
                hashAlgorithm.Initialize();
                mode = FileAccess.Write;
                final = false;
            }

            innerStream.Write(buffer, offset, count);
            hashAlgorithm.TransformBlock(buffer, offset, count, null, 0);
        }

		protected override void Dispose(bool disposing)
		{
			if (disposing && innerStream != null)
            {
				if (!leaveInnerStreamOpen)
					innerStream.Close();
				innerStream = null;
			}
		}

        public byte[] Checksum
        {
            get {
                if (final == false)
                {
                    byte[] buffer = new byte[1];
                    hashAlgorithm.TransformFinalBlock(buffer, 0, 0);
                    final = true;
                }
                return hashAlgorithm.Hash;
            }
        }

        public void Reset()
        {
            mode = FileAccess.ReadWrite;
            final = false;
        }
    }
}

